package cn.ainit.bj.isbn;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.bj.isbn.entity.ISBNApply;
import cn.ainit.common.ReturnObject;
import cn.ainit.sys.utils.UUID32;

@Controller
@RequestMapping("/isbnApply")
public class ISBNApplyController {
	public ISBNApplyService isbnApplyService;
	@Resource
	public void setIsbnApplyService(ISBNApplyService iSBNApplyService) {
		this.isbnApplyService = iSBNApplyService;
	}
	/**
	 * 保存和修改
	 * @param isbnApply
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-2-11 下午03:16:20
	 */
	@RequestMapping("/saOrUpdISBN")
	@ResponseBody
	public ReturnObject saveISBNApply(ISBNApply isbnApply,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			boolean flag = false;
			String isbnId = "";
			if (isbnApply.getIsbnId()==null || isbnApply.getIsbnId().equals("")|| isbnApply.getIsbnId().equals("null")) {
				//保存
				isbnId = UUID32.newGuid();
				isbnApply.setIsbnId(isbnId);
				flag = isbnApplyService.saveISBNApply(isbnApply);
				if (flag) {
					returnObject.setMsg("success");
					returnObject.setInfo(isbnId);
				}else{
					returnObject.setMsg("fail");
				}
			}else {
				//修改
				flag = isbnApplyService.updISBNApply(isbnApply);
				if (flag) {
					returnObject.setMsg("upsuccess");
				}else {
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return returnObject;
	}
	/**
	 * 点击审核按钮 调用的方法
	 * @param isbnId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-2-12 上午11:06:11
	 */
	@RequestMapping("/execISBNApply")
	@ResponseBody
	public ReturnObject  execISBNApply(String isbnId,HttpServletRequest request) throws Exception{
		//前台页面要对isverify 进行判断  后台还要再判断  以防后台有人操作出现问题
		//根据Id得到这个实体类  在进行判断和获取所需的参数                            //0为保存状态    1为审批完成   2 为审批中    
		ReturnObject returnObject = new ReturnObject();
		try {
			ISBNApply isbnApply = this.isbnApplyService.toUpdateISBN(isbnId);
			String isverify = isbnApply.getIsVerify();
			String djId = isbnApply.getIsbnId();
			String djCode = isbnApply.getDjCode();
			String djType = isbnApply.getDjType();
			if (isverify != null && (isverify.equals("0") || isverify == "0")) {
				//执行函数
				String sql = "select pk_com_approve.uf_GetDjApprove('"+djId+"','"+djCode+"','"+djType+"') from dual ";
				String shuHaoValue = this.isbnApplyService.updISBNVerify(sql);
				if (shuHaoValue.equals("0")|| shuHaoValue == "0") {
					String sql2 = "update bj_isbnApply set IS_VERIFY = 1 where ISBN_ID = '"+isbnId+"'";
					if (this.isbnApplyService.execSql(sql2)) {
						returnObject.setTraState(4);               //直接将is_verify 改为1  表示不需要审批  前台应应标识为审核按钮
						returnObject.setInfo("0");
					}
				}else {
					String sql4 = "update bj_isbnApply set IS_VERIFY = 2 where ISBN_ID = '"+isbnId+"'";
					if (this.isbnApplyService.execSql(sql4)) {
						returnObject.setTraState(2);			   //表示为审批中
						returnObject.setInfo("1");            //打开审批
					}
				}
			}else if(isverify != null && (isverify.equals("1")|| isverify == "1")){
				String sql3 = "select count(1) from sys_approve where DJ_ID ='"+isbnId+"'";
				int number = this.isbnApplyService.execSqlApply(sql3);
				if (number >0) {
					returnObject.setInfo("1");
					returnObject.setTraState(3);                  //审批完成，并且审批表中存在审批记录
				}else {
					returnObject.setTraState(4);                  
					returnObject.setInfo("0");
				}
			}else {
				returnObject.setInfo("1");
			}
			returnObject.setMsg("success");
		} catch (Exception e) {
			returnObject.setMsg("fail");
			e.printStackTrace();
		}
		return returnObject;
	}
	
	/**
	 * 页面双击数据加载按钮时  如果审批状态为1的话进来判断
	 * @param isbnId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-2-14 下午03:15:57
	 */
	@RequestMapping("/judgeVerify")
	@ResponseBody
	public ReturnObject judgeStatus(String isbnId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String sql3 = "select count(1) from sys_approve where DJ_ID ='"+isbnId+"'";
			int number = this.isbnApplyService.execSqlApply(sql3);
			if (number >0){
				returnObject.setMsg("1");
			}else {
				returnObject.setMsg("0");
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	
	/**
	 * 执行生成商品资料的存储过程
	 * @param isbnId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-2-14 下午02:21:36
	 */
	@RequestMapping("/execJudge")
	@ResponseBody
	public ReturnObject execProduct(String isbnId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String number = this.isbnApplyService.execISBNApply(isbnId);
			 returnObject.setMsg(number);
			 returnObject.setInfo("H_ID-"+number);
		} catch (Exception e) {
			returnObject.setErr(e.getMessage());
		}
		return returnObject;
	}
	
	@RequestMapping("/saveISBN")
	@ResponseBody
	public ReturnObject saveISBN(@RequestBody ISBNApply singleObj, HttpServletRequest request){
		ReturnObject returnObject =  new ReturnObject();
		try {
			System.out.println("保存ISBN数据");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
}

